using System;
using System.Collections.Generic;
using System.Text;

namespace Axe.Workflow
{
    [Serializable]
    public class StatisticTimimgTask : Task, ITaskStatistic
    {
        private DateTime mUptime;
        private DateTime mStopTime;
        private DateTime mStartTime;
        private DateTime mSuspendTime;

        private TimeSpan mOnline;
        private TimeSpan mOffline;
        private TimeSpan mSuspend;

        private DateTime mLastOpTime;

        /// <summary>
        /// Gets the last stop time
        /// </summary>
        /// <value>The last stop.</value>
        public DateTime LastStop
        {
            get
            {
                return mStopTime;
            }
        }

        /// <summary>
        /// Gets the last start time
        /// </summary>
        /// <value>The last start.</value>
        public DateTime LastStart
        {
            get
            {
                return mStartTime;
            }
        }

        /// <summary>
        /// Gets the total time passed until task was created.
        /// </summary>
        /// <value>The uptime.</value>
        public TimeSpan Uptime
        {
            get
            {
                return DateTime.Now - mUptime;
            }
        }

        /// <summary>
        /// Gets the total time passed until task was created including all the time,
        /// when task was running (not paused).
        /// </summary>
        public TimeSpan Online
        {
            get
            {
                TimeSpan result = mOnline;
                if(IsRunning && !IsPaused)
                    result += DateTime.Now - mLastOpTime;
                return result;
            }
        }

        /// <summary>
        /// Gets the total time passed until task was created including all the time,
        /// when task was stopped (not paused).
        /// </summary>
        public TimeSpan Offline
        {
            get
            {
                TimeSpan result = mOffline;
                if(!IsRunning && !IsPaused)
                    result += DateTime.Now - mLastOpTime;
                return result;
            }
        }

        /// <summary>
        /// Gets the total time passed until task was created including all the time,
        /// when task was paused (not running and not stopped).
        /// </summary>
        public TimeSpan Suspended
        {
            get
            {
                TimeSpan result = mSuspend;
                if(IsPaused)
                    result += DateTime.Now - mLastOpTime;
                return result;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StatisticTimimgTask"/> class.
        /// </summary>
        public StatisticTimimgTask()
        {
            OnStarting += new ExecutionStateChanging(StatisticTimimgTask_OnStart);
            OnStopping += new ExecutionStateChanging(StatisticTimimgTask_OnStop);
            OnSuspending += new ExecutionStateChanging(StatisticTimimgTask_OnSuspend);
            OnResuming += new ExecutionStateChanging(StatisticTimimgTask_OnResume);

            mUptime = DateTime.Now;
            mStopTime = mUptime;
            mLastOpTime = mUptime;
        }

        bool StatisticTimimgTask_OnResume()
        {
            mStartTime = DateTime.Now;
            mSuspend += mStartTime - mSuspendTime;
            mLastOpTime = mStartTime;

            return true;
        }

        bool StatisticTimimgTask_OnSuspend()
        {
            mSuspendTime = DateTime.Now;
            mLastOpTime = mSuspendTime;
            mOnline += mSuspendTime - mStartTime;

            return true;
        }

        bool StatisticTimimgTask_OnStop()
        {
            mStopTime = DateTime.Now;
            mOnline += mStopTime - mStartTime;
            mLastOpTime = mStopTime;

            return true;
        }

        bool StatisticTimimgTask_OnStart()
        {
            mStartTime = DateTime.Now;
            mOffline += mStartTime - mStopTime;
            mLastOpTime = mStartTime;

            return true;
        }

    }
}
